﻿var LogView = LogView || {};
LogView.View = LogView.View || {};

$.extend(LogView.View, function () {

    function renderView() {

        var params = { id: LogView.QueryString('id'), filters: [] };

        // Find all the filters on the page
        $('[data-filtertype]').each(function (indx, elem) {

            $elem = $(elem);
            var filterItem = { TypeName: $elem.attr('data-filtertype'), SqlColumn: $elem.attr('data-sqlcolumn') };
            populateFilterData(filterItem, $elem);
            params.filters.push(filterItem);
        });

        // Refresh the view with the filter parameters
        LogView.UpdatePartial("view.aspx/Render", params, "#resultView");
    }

    function populateFilterData(filterItem, $elem) {

        switch (filterItem.TypeName) {

            case "FilterDateRange":
                filterItem.Data = $elem.find('#dateStart').val() + " " + $elem.find('#dateEnd').val();
                break;

            case "FilterSite":
                filterItem.Data = JSON.stringify(filterSiteId());
                break;

            case "FilterString":
                filterItem.Data = $elem.find('#text').val();
                break;

        }
    }

    function filterSiteId() {

        // Filter Site List
        var sites = [];
        $("#siteFilter div[data-item][selected=selected]").each(function (indx, elem) {
            var siteId = $(elem).attr('data-item');
            if (siteId != 'all')
                sites.push(siteId);
        });

        return sites;
    }

    return {

        RenderView: renderView
    };

} ());

